<template>
  <div class="process-main">
    <div class="topBox add_btn_top">
      <div class="switch-text" :style="{ color: switchTextColor }">
        {{ switchText }}
      </div>
      <el-switch
        v-permission="[1, 'switch']"
        class="screenSwitch"
        active-color="#2680D2"
        @change="openScrenn"
        v-model="screen"
      >
      </el-switch>
      <div class="line"></div>
      <el-button
        v-permission="[1, 'button']"
        type="primary"
        size="mini"
        @click="handAdd"
        ><i class="el-icon-plus"></i>添加
      </el-button>
      <el-button class="button" size="mini">
        <svg-icon class="svg-icon" icon-class="import" />导入
      </el-button>
    </div>
    <div class="boxLayout">
      <div
        v-permission="[4, 'list']"
        :style="{
          width: screen ? '75%' : '100%',
          background: '#fff',
        }"
      >
        <el-table
          ref="topicTable"
          v-loading="loading"
          class="cell_padding_0"
          :tree-props="{ children: 'children' }"
          row-key="id"
          default-expand-all
          :data="tableData.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
          height="100px"
          v-adaptive
          style="width: 100%"
          border
        >
          <el-table-column prop="parentIndex" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="partNumber" label="零件号" width="120">
            <template slot-scope="scope">
              <span v-if="scope.row.type != 2" style="cursor: pointer">
                {{ scope.row.partNumber }}
              </span>
              <!-- <div v-else class="type_line">
                <div class="type_line-last" v-if="scope.row.last == -1"></div>
                <div class="type_line-1" v-else></div>
                <div class="type_line-2"></div>
              </div> -->
            </template>
          </el-table-column>
          <el-table-column prop="partName" label="零件名称">
            <template slot-scope="scope">
              <span v-if="scope.row.type != 2">
                {{ scope.row.partName }}
              </span>
              <!-- <div v-else class="type_line">
                <div class="type_line-2"></div>
                <div
                  class="el-icon-caret-right"
                  style="color: #ffa0de; margin-top: 1px"
                ></div>
              </div> -->
            </template>
          </el-table-column>
          <el-table-column prop="processNumber" label="工序号">
            <template slot-scope="scope">
              <span>
                {{ scope.row.processNumber }}
              </span>
              <!-- <div v-else class="type_line">
                <div class="type_line-2"></div>
                <div
                  class="el-icon-caret-right"
                  style="color: #ffa0de; margin-top: 1px"
                ></div>
              </div> -->
            </template>
          </el-table-column>
          <el-table-column
            prop="processName"
            label="工序名称"
          ></el-table-column>

          <el-table-column
            width="120"
            prop="version"
            label="模板"
            v-if="isTabShow('模板')"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{
                scope.row &&
                scope.row.technologyTemplate &&
                scope.row.technologyTemplate.name
              }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="isTabShow('工艺版次')"
            prop="version"
            label="工艺版次"
            width="80"
          ></el-table-column>

          <el-table-column
            prop="state"
            label="状态"
            width="80"
            v-if="isTabShow('状态')"
          >
            <template slot-scope="scope">
              <span
                :style="scope.row.state == 'false' ? 'color: #999999;' : ''"
              >
                {{ formatState(scope.row.state) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createdDate"
            label="创建时间"
            width="180"
            v-if="isTabShow('创建时间')"
          >
            <template slot-scope="scope">
              {{ scope.row.createdDate | formatDate }}
            </template>
          </el-table-column>
          <el-table-column
            prop="endDate"
            label="有效期"
            width="180"
            v-if="isTabShow('有效期')"
          >
            <template slot-scope="scope">
              {{ scope.row.endDate | formatDate }}
            </template>
          </el-table-column>
          <!-- <div v-for="(item, i) in otherCol" :key="i">
            <el-table-column
              :label="item.label"
              width="180"
              v-if="isTabShow(item.label)"
            >
              <template slot-scope="scope">
                {{
                  technologyParameter(scope.row.technologyParameter)[i].value
                }}
              </template>
            </el-table-column>
          </div> -->
          <el-table-column
            prop="author"
            label="创建人"
            v-if="isTabShow('创建人')"
          ></el-table-column>
          <el-table-column align="center" fixed="right">
            <!-- eslint-disable-next-line -->
            <template slot="header" slot-scope="scope">
              <div class="flex-x flex-space">
                <div>操作</div>
                <el-popover width="100" trigger="click">
                  <div class="column_search_box">
                    <div class="column_title">自定义表头字段</div>
                    <el-checkbox-group v-model="colCheckList">
                      <div class="flex-y column_search">
                        <el-checkbox
                          :label="item.label"
                          v-for="item in getColumnList"
                          :key="item.label"
                        >
                          {{ item.label }}</el-checkbox
                        >
                      </div>
                    </el-checkbox-group>
                  </div>
                  <span slot="reference" class="el-icon-s-grid"></span>
                </el-popover>
              </div>
            </template>
            <template slot-scope="scope">
              <div class="table_btns flex-x flex-space">
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-edit-outline"
                  @click="handEdit(scope.row.token, scope.$index)"
                ></el-button>
                <el-button
                  v-permission="[2, 'button']"
                  type="text"
                  size="mini"
                  icon="el-icon-delete"
                  @click="handDelete(scope.row.token, scope.$index)"
                ></el-button>
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-view"
                  @click="handNavTo(scope.row.token, scope.$index)"
                ></el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :page.sync="pageNum"
          :limit.sync="pageSize"
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page.sync="pageNum"
          :page-size="pageSize"
          :total="tableData.length"
        />
      </div>
      <SearchFilter
        :tableData="tableData"
        :templateList="templateList"
        v-show="screen"
        @setFilterList="setFilterList"
        :userList="userList"
      ></SearchFilter>
    </div>
    <AddDialog
      :key="generateId"
      :mainToken="mainToken"
      :showDialog.sync="showDialog"
      :isedit="isedit"
      :title="title"
      @closeDialog="closeDialog"
      ref="addDialogRef"
    ></AddDialog>
  </div>
</template>

<script>
const COLUMN_LIST = [
  {
    label: "模板",
  },
  {
    label: "工艺版次",
  },
  {
    label: "状态",
  },
  {
    label: "创建时间",
  },
  {
    label: "有效期",
  },
  {
    label: "创建人",
  },
];
import { spareParts } from "@/utils/local";
import Pagination from "@/views/common/page";
import SearchFilter from "./searchFilter.vue";
import AddDialog from "./AddDialog.vue";
import { generateId } from "@/utils/index";
import { paginationMixins } from "@/mixin/pagination";
import { getUserList } from "@/api/userAuth";
import {
  getTechnology,
  technologyTokens,
  delTechnology,
  getTemplate,
  getTechnologyToken,
} from "@/api/technology";
export default {
  mixins: [paginationMixins],
  components: {
    Pagination,
    SearchFilter,
    AddDialog,
  },

  data() {
    return {
      columnList: COLUMN_LIST,
      colCheckList: COLUMN_LIST.map((item) => item.label),
      loading: false,
      total: 0,
      pageNum: 1,
      pageSize: 10,
      title: "主工艺",
      screen: false,
      screenShow: false,
      switchTextColor: "#666666",
      switchText: "显示筛选",
      filterData: "",
      showDialog: false,
      isedit: false,
      editmetaData: {},
      editmetaData: true,
      mainToken: "",
      otherCol: [],
      // 树结构表格必须指定id (注意不能为0)
      tableData: [],
      templateList: [],
      form: {
        author: "", //当前用户
        state: "", //状态
        name: "", //名称
        templateId: "", //模板id
        url: "", //模板的url
      },
      userList: [],
    };
  },
  computed: {
    technologyParameter() {
      return function (arr) {
        return arr.filter((item) => !spareParts.includes(item.key));
      };
    },
    // COLUMN_LIST
    getColumnList() {
      // console.log("this.tableData :>> ", this.tableData);
      return COLUMN_LIST;
      // if (this.tableData.length) {
      //   return COLUMN_LIST.concat(this.otherCol);
      // } else {
      //   return COLUMN_LIST;
      // }
    },
    generateId() {
      return generateId();
    },
    isTabShow() {
      return function (name) {
        return this.colCheckList.includes(name);
      };
    },
  },
  async mounted() {
    this.init();
    this.getTemplateList();
    var that = this;
    const res = await getUserList();
    const { data } = res.data;
    that.userList = data;
  },
  methods: {
    handNavTo(token) {
      this.$router.push({
        name: "mainExhibition",
        query: {
          token,
        },
      });
    },
    //获取模板列表
    getTemplateList() {
      var params = {
        page: 1,
        pageSize: 0,
      };
      getTemplate(params).then((res) => {
        const { results } = res.data;
        this.templateList = results;
      });
    },
    formatDateT(date) {
      return formatTime(date);
    },
    formatState(data) {
      if (data == "true") {
        return "可用";
      } else if (data == "false") {
        return "禁用";
      }
    },
    //筛选调用
    setFilterList(data) {
      if (data) {
        this.switchTextColor = "#2680D2";
        this.switchText = "显示筛选 ( 已筛选 )";
      } else {
        this.switchTextColor = "#666666";
        this.switchText = "显示筛选";
      }
      this.filterData = data;
      this.init();
    },
    init() {
      this.loading = true;
      this.params = {
        page: 1,
        pageSize: 20,
        includeTemplate: true,
        // includeFormula: true,
      };
      if (this.filterData) {
        this.params = {
          ...this.params,
          ...this.filterData,
        };
      }
      getTechnology(this.params)
        .then((r) => {
          let { results, numResults } = r.data;
          this.total = numResults;
          this.tableData = results;
          // this.tableData.forEach((item) => {
          //   item.children.forEach((child) => {
          //     // child.children = unique1(item);
          //     this.$set(child, "children", unique1(results, "processNumber"));
          //   });
          // });
          // if (this.tableData.length) {
          //   const { technologyParameter } = this.tableData[0];
          //   this.otherCol = technologyParameter.map((item) => {
          //     return {
          //       label: item.key,
          //     };
          //   });
          // }
          // this.otherCol = this.otherCol.filter(
          //   (item) => !spareParts.includes(item.label)
          // );
          console.log(" 表格整理树结构 :>> ", this.tableData);
        })
        .finally((f) => {
          this.loading = false;
        });
    },
    indexMethod(index) {
      return index + 1;
    },
    // 显示筛选窗
    openScrenn() {},
    //添加
    handAdd() {
      this.showDialog = true;
    },
    //编辑
    handEdit(token) {
      this.showDialog = true;
      this.mainToken = token;
      this.isedit = true;
    },
    //删除
    handDelete(token) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delTechnology(token).then((res) => {
            this.$msg({
              type: "success",
              message: "删除成功!",
            });
            this.init();
          });
        })
        .catch((c) => {});
    },
    closeDialog(isRefresh) {
      // isRefresh是否刷新
      if (isRefresh) {
        this.init();
      }
      this.showDialog = false;
      this.isedit = false;
    },
    sizeChange(val) {
      console.log("val", val);
      this.pageSize = val;
      this.pageNum = 1;
    },
    currentChange(val) {
      console.log("currentChangeval", val);
      this.pageNum = val;
    },
  },
};
</script>

<style lang='scss' scoped>
@import "~@/assets/style/processCss/baseStyle.css";
.column_search_box {
  .column_title {
    font-weight: 600;
    margin-bottom: 10px;
    font-size: 15px;
  }
  .column_search {
    /deep/ .el-checkbox {
      margin-right: 0;
      margin-bottom: 10px;
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}
.table_btns {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  height: 100%;
}
.cell_padding_0 {
  /deep/ .el-table__expand-icon {
    display: none !important;
  }
  /deep/ .el-table__row--level-1 {
    td {
      padding: 0;
      /* border-bottom: 0; */
    }
    .type_line {
      display: flex;
      align-items: center;
      /* padding-left: 20px; */
      height: 48px;
      .type_line-1 {
        margin-left: 20px;
        height: 48px;
        /* width: 1px; */
        border-left: 1px dashed #ffa0de;
        /* background-color: #ffa0de; */
      }
      .type_line-last {
        margin-left: 20px;
        height: 24px;
        width: 1px;
        /* background-color: #ffa0de; */
        border-left: 1px dashed #ffa0de;
        align-self: flex-start;
      }
      .type_line-2 {
        height: 1px;
        width: 90px;
        /* background-color: #ffa0de; */
        border-top: 1px dashed #ffa0de;
      }
    }

    .cell {
      height: 48px;
      /* padding: 0; */
      line-height: 48px;
    }
    /* td:nth-child(3),
    td:nth-child(2) {
      .cell {
        padding: 0 !important;
      }
    } */
    .el-table__indent,
    .el-table__placeholder {
      display: none;
    }
  }
  /* /deep/ tbody .cell {
    padding: 0 !important;
  } */
}
.row_name {
  color: #409eff;
  cursor: pointer;
}
</style>
